#include <assert.h>
#include "humantextplayer.h"
namespace{
    void convert(char& ch, int upper)
    {
        if(ch >= 'a' && ch < 'a' + upper){
            ch = ch -'a' + 1;
        }
        else if(ch >= 'A' && ch < 'A' + upper){
            ch = ch - 'A' + 1;
        }
        else if(ch >= '1' && ch < '1' + upper){
            ch = ch - '1' + 1;
        }
    }
    bool in_board(int row, int col)
    {
        return (row > 0) && (row <= Board::ROWS)
            && (col > 0) && (col <= Board::COLS);
    }
    void print_color(ostream& output, Color color)
    {
        if(color == Black)
            output << "Black";
        else
            output << "White";
    }
}
void HumanTextPlayer::get_move(Board const& board, int& r, int& c)
{
    char row, col;
    do{
        print_color(output_, color_);
        output_ << ", enter your move (e.g., a7) : ";
        input_ >> row >> col;
        convert(row, Board::ROWS);
        convert(col, Board::COLS);
        if(in_board(row, col)
           &&board.valid_move(color_, row, col)){
            break;
        }
        board.get_possible_move(color_, r, c);
        output_ << "Invalid move. Please try again.\n"
                << char('a' + r -1) << char('1' + c -1)
                <<" is a valid example, but other moves may be better.\n";
    }while(1);
    r = row;c = col;
}
void HumanTextPlayer::skip_move(Board const& )
{
    print_color(output_, color_);
    output_ << ": no valid move possible.\n";
}
void HumanTextPlayer::win()
{
    print_color(output_, color_);
    output_ << " wins!\n";
}
void HumanTextPlayer::tie()
{
    if(color_ == Black){
        output_ << "Game tied.\n";
    }
    else{
        output_ << "No winner!\n";
    }
}

    

    
        
    
        

            
